package com.pengweiwei.androidzygote.functions.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;

import com.google.gson.annotations.SerializedName;
import com.pengweiwei.androidzygote.BR;
import com.pengweiwei.androidzygote.R;
import com.putong35.common.baseactfrag.activity.PullableListActivity;
import com.putong35.common.baseactfrag.adapter.DataBindRecyclerViewAdapter;
import com.putong35.common.baseactfrag.adapter.RecyclerViewBaseAdapter;
import com.putong35.common.httprequest.Response;
import com.putong35.common.utils.io.AssetsUtil;

import java.util.List;


/**
 * 刷列表的模板
 * <p>
 * 主要分为三步：fragment  + adapter(+ layout布局)  + bean
 * <p>
 * 2. adapter要设置 泛型参数（LoginResponce）  e.g extends RecyclerViewBaseAdapter<LoginResponce>
 */
public class DataBindListActivity extends PullableListActivity {

    public static void start(Context context) {
        Intent starter = new Intent(context, DataBindListActivity.class);
        context.startActivity(starter);
    }

    //    private final static String URL_TEST = "http://hpapi.baoruan.com:10081/test/list";
    private final static String URL_TEST = "http://apitest.baoruan.com/launcher/default/index?version=9.150320&un=239BA6301606E596F70AC1E7B4808B23&name=recommendlist&channelId=ceshi_baoruanservice1";

    private int mPage = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("DataBindListActivity");
        init();
    }

    private void init() {
        mPage = 0;
        clearDecorations();
        setPullToRefreshEnable(false);
    }

    @Override
    public void onResume() {
        super.onResume();
        startLoading();
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long itemId) {
        DataBindListActivity.start(this);
    }

    //    private ListDataRequestListener<TemplateBean> mResponceListener = new ListDataRequestListener<>(this, TemplateBean.class);

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new DataBindRecyclerViewAdapter<TemplateBean>(this, R.layout.listitem_template_data_bind, BR.itemBean);
    }


    @Override
    public void onStartLoading() {
        String responseStr = AssetsUtil.getStringFromAssets(this, "list_ui_test_data.txt");
        Response parser = new Response(responseStr);
        List<TemplateBean> list = parser.getDataListBean(TemplateBean.class);
        if (list != null && list.size() > 0) {
            getAdapter().getData().addAll(list);
        }

    }

    /**
     * 数据对象 Bean
     */
    public static class TemplateBean extends BaseObservable {
        @SerializedName("iconUrl")
        String iconUrl;
        @SerializedName("name")
        String name;

        private int id;
        private double price;
        private List<String> imageList;
        @Bindable
        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
            notifyPropertyChanged(BR.id);
        }
        @Bindable
        public double getPrice() {
            return price;
        }

        public void setPrice(double price) {
            this.price = price;
            notifyPropertyChanged(BR.price);
        }
        @Bindable
        public List<String> getImageList() {
            return imageList;
        }

        public void setImageList(List<String> imageList) {
            this.imageList = imageList;
            notifyPropertyChanged(BR.imageList);
        }

        @Bindable
        public String getIconUrl() {
            return iconUrl;
        }

        public void setIconUrl(String iconUrl) {
            this.iconUrl = iconUrl;
            notifyPropertyChanged(BR.iconUrl);
        }

        @Bindable
        public String getName() {
            return name;
        }

        public void setName(String text) {
            this.name = text;
            notifyPropertyChanged(BR.name);
        }
    }

}
